<template>
    <div>
        <div class="title">
            <div class="back" @click="$router.back()">
                <span @click="change2"> <img src="../assets/img/fanhuiblack.png" alt=""> </span>
            </div>
            <div class="gamename">杉果资讯</div>
        </div>
        <div class="tab">
            <van-tabs v-model="active" animated :title-active-color="'#85858c'">
                <van-tab :title="'推荐'">

                    <div class="item" v-for="(item, index) in newList" :key="index">
                        <div class="text_box">
                            <div class="top">
                                <p>{{ item.title }}</p>
                            </div>
                            <div class="bottom">
                                <p class="text">
                                    <span class="type">#{{ item.type }}</span>
                                    <span class="time">{{ item.created_at_timestamp | time }}
                                    </span>
                                </p>
                                <p class="good">
                                    <span class="icon">
                                        <img v-if="goodList.indexOf(item.id) === -1"
                                            @click="good(item.id), item.likes_count++" src="../assets/img/good.png" alt="">
                                        <img v-else @click="delGood(item.id), item.likes_count--"
                                            src="../assets/img/gooded.png" alt="">
                                    </span>

                                </p>
                            </div>
                        </div>
                        <router-link class="pic_box" tag="div" :to="'/headLineDetail?id=' + item.id">

                            <img referrerPolicy="no-referrer" :src="item.thumbnail.url" alt="" @click="change">

                        </router-link>
                    </div>
                </van-tab>
                <van-tab :title="'资讯'">
                    <div class="item" v-for="(item, index) in infoList" :key="index">
                        <div class="text_box">
                            <div class="top">
                                <p>{{ item.title }}</p>
                            </div>
                            <div class="bottom">
                                <p class="text">
                                    <span class="type">#{{ item.type }}</span>
                                    <span class="time">{{ item.created_at_timestamp | time }}
                                    </span>
                                </p>
                                <p class="good">
                                    <span class="icon">
                                        <img v-if="goodList.indexOf(item.id) === -1"
                                            @click="good(item.id), item.likes_count++" src="../assets/img/good.png" alt="">
                                        <img v-else @click="delGood(item.id), item.likes_count--"
                                            src="../assets/img/gooded.png" alt="">
                                    </span>

                                </p>
                            </div>
                        </div>

                        <router-link class="pic_box" tag="div" :to="'/headLineDetail?id=' + item.id">

                            <img referrerPolicy="no-referrer" :src="item.thumbnail.url" alt="" @click="change">

                        </router-link>
                    </div>
                </van-tab>
                <van-tab :title="'文章'">
                    <div class="item" v-for="(item, index) in infoList" :key="index">
                        <div class="text_box">
                            <div class="top">
                                <p>{{ item.title }}</p>
                            </div>
                            <div class="bottom">
                                <p class="text">
                                    <span class="type">#{{ item.type }}</span>
                                    <span class="time">{{ item.created_at_timestamp | time }}
                                    </span>
                                </p>
                                <p class="good">
                                    <span class="icon">
                                        <img v-if="goodList.indexOf(item.id) === -1"
                                            @click="good(item.id), item.likes_count++" src="../assets/img/good.png" alt="">
                                        <img v-else @click="delGood(item.id), item.likes_count--"
                                            src="../assets/img/gooded.png" alt="">
                                    </span>

                                </p>
                            </div>
                        </div>
                        <router-link class="pic_box" tag="div" :to="'/headLineDetail?id=' + item.id">

                            <img referrerPolicy="no-referrer" :src="item.thumbnail.url" alt="" @click="change">

                        </router-link>
                    </div>
                </van-tab>
                <van-tab :title="'视频'">
                    <div class="item" v-for="(item, index) in infoList" :key="index">
                        <div class="text_box">
                            <div class="top">
                                <p>{{ item.title }}</p>
                            </div>
                            <div class="bottom">
                                <p class="text">
                                    <span class="type">#{{ item.type }}</span>
                                    <span class="time">{{ item.created_at_timestamp | time }}
                                    </span>
                                </p>
                                <p class="good">
                                    <span class="icon">
                                        <img v-if="goodList.indexOf(item.id) === -1"
                                            @click="good(item.id), item.likes_count++" src="../assets/img/good.png" alt="">
                                        <img v-else @click="delGood(item.id), item.likes_count--"
                                            src="../assets/img/gooded.png" alt="">
                                    </span>

                                </p>
                            </div>
                        </div>
                        <router-link class="pic_box" tag="div" :to="'/headLineDetail?id=' + item.id">

                            <img referrerPolicy="no-referrer" :src="item.thumbnail.url" alt="" @click="change">

                        </router-link>
                    </div>
                </van-tab>
            </van-tabs>
        </div>
        <div class="bottom" style="height: 50px
        ;"></div>
        <fotter-bar></fotter-bar>

    </div>
</template>
<script>
import { getNews, getInfo } from '../api/newView'
import fotterBar from '../components/fotterBar.vue'
import { Toast } from 'vant'
export default {
    data() {
        return {
            newList: [],//新闻列表
            active: 0,
            goodList: JSON.parse(localStorage.getItem('goodList')) || [],//点赞列表
            infoList: [],//信息列表
            // titleId=
        }
    }, components: {
        fotterBar
    }, methods: {
        change() {
            this.$store.commit('change', false)
        }, change2() {
            this.$store.commit('change', true)
        }, getNews() {
            getNews().then(data => {
                this.newList = data
            })
        }, good(id) {
            // 点赞功能
            if (window.localStorage.getItem('token') == undefined) {
                Toast('请先登录，点再赞')
                this.$router.push('/userLogin')
                return
            }
            else {
                this.goodList.push(id)
                window.localStorage.setItem('goodList', JSON.stringify(this.goodList))
            }

        },
        delGood(id) {
            // 取消点赞功能
            let index = this.goodList.findIndex(item => {
                return item === id
            })
            this.goodList.splice(index, 1)
            window.localStorage.setItem('goodList', JSON.stringify(this.goodList))
        }, getInfo() {
            getInfo().then(data => {
                this.infoList = data
            })
        }
    }, filters: {
        // 过滤时间
        time(value) {
            return new Date(value).toLocaleString().slice(0, 10);

        }
    },
    created() {
        this.getNews()
        this.getInfo()

    }
}

</script>

<style lang="scss" scoped>
.item {
    display: flex;
    padding: 0 12px 10px;
    margin-top: 20px;
    border-bottom: 1px solid #f7f7fb;

    .text_box {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        margin-right: 10px;

        .top {
            p {
                font-size: 12px;
            }

        }

        .bottom {
            .good {
                span {
                    img {
                        width: 15px;
                        height: 15px;
                        display: inline-block;
                    }
                }
            }

            display: flex;
            justify-content: space-between;

            .text {
                .type {
                    font-size: 12px;
                    color: #025cb8;
                }

                .time {
                    font-size: 12px;
                    margin-left: 10px;
                    color: #c3c3c6;
                }
            }

        }
    }

    .pic_box {
        width: 113px;
        height: 70px;

        img {
            width: 100%;
            height: 100%;
            border-radius: 5px;
        }
    }

}

.tab {
    & ::v-deep .van-tabs__line {
        background-color: #85858c;
        width: 20px;
        height: 1px;

    }
}



.title {
    position: relative;
    z-index: 2;
    display: flex;
    background-color: #ffffff;
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0 12px;
    color: #222222;
    justify-content: space-between;
    box-sizing: border-box;
    z-index: 2;
    top: 0;
    border-bottom: 2px solid #f7f7fb;

    .back {
        display: flex;
        align-items: center;

        span {
            font-size: 16px;

            img {
                width: 16px;
                height: 16px;
            }
        }
    }

    .gamename {
        flex: 1;
        text-align: center;
        font-size: 17px;
    }

    .more {
        span {
            font-size: 17px;
        }
    }



}
</style>

